#import <objc/runtime.h>
#import "MRSkinManager.h"
#import "UITableView+Extension.h"

@implementation UITableViewCell (Extension)

+ (void)load
{
	Method fun = class_getInstanceMethod(self, @selector(layoutSubviews));
	Method new = class_getInstanceMethod(self, @selector(fsLayoutSubviews));
	method_exchangeImplementations(fun, new);
}

- (void)awakeFromNib
{
	[super awakeFromNib];
	
	//实现特定风格（用户资料或圈子选项面，标题较小选项较大时的颜色）
	if (self.textLabel && self.detailTextLabel && self.detailTextLabel.font.pointSize > self.textLabel.font.pointSize)
	{
		self.textLabel.alpha = .5;
		self.detailTextLabel.textColor = [MRSkinManager skin].mainTextColor;
	}
	
	//全局自定右箭头
	if (self.accessoryType == UITableViewCellAccessoryDisclosureIndicator)
	{
		self.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_cell_arrow"]];
	}
}

- (void)didLanguage
{
	self.textLabel.text = L(self.language);
}

//隐藏组上边与下边线
- (void)fsLayoutSubviews
{
	[self fsLayoutSubviews];
	
	//过滤相册选项单元格背景颜色
	if ([NSStringFromClass(self.class) isEqualToString:@"PUAlbumListTableViewCell"])
	{
		self.backgroundColor = [UIColor clearColor];
		return;
	}
	
	UITableView *tv = (UITableView *)self.superview.superview;
	if (![tv isKindOfClass:[UITableView class]]) return;
	if (tv.style == UITableViewStylePlain) return;
	
	NSIndexPath *path = [tv indexPathForCell:self];
	NSUInteger rows = [tv numberOfRowsInSection:path.section];
	
	NSMutableArray <__kindof UIView *> *cells = [NSMutableArray array];
	
	[self viewWithClass:NSClassFromString(@"_UITableViewCellSeparatorView") handler:^(UIView *view, BOOL *stop) {
		[cells addObject:view];
	}];
	
	if ((path.row == rows - 1) && (cells.count >= 2)) cells[cells.count - 2].hidden = YES;
	if (cells.count == 3) cells[0].hidden = YES;
}

@end

#pragma mark - 表格视图
@implementation UITableView (Extension)

- (void)awakeFromNib
{
	[super awakeFromNib];
	
	self.contentInset = self.scrollIndicatorInsets;
	//self.backgroundColor = [UITableView appearance].backgroundColor;
	
	self.sectionHeaderHeight = self.sectionFooterHeight = LOCPIXEL(DPISCALE(10));
}

- (id)cellAtSection:(NSUInteger)section row:(NSUInteger)row
{
	return [self cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];
}

- (id)cellContainView:(UIView *)view
{
	while (view && ![view isKindOfClass:[UITableViewCell class]])
	{
		view = view.superview;
	}
	
	return view;
}

/*===================================================================
 说明:
	设置表头对应屏幕的宽高比，一般在用于封面时设置
 ==================================================================*/
- (void)setTableHeaderViewAspectRatio:(CGFloat)value
{
	CGRect frame = [UIScreen mainScreen].bounds;
	frame.size.height = LOCPIXEL(frame.size.width * value);
	
	self.tableHeaderView.frame = frame;
}

/*===================================================================
 说明:
	设置无数据时表内容
 ==================================================================*/
- (void)setNoDataWithTitle:(NSString*)title
{
    if (self.backgroundView == nil)
    {
        self.backgroundView = [[UIView alloc] initWithFrame:self.bounds];
        self.backgroundView.backgroundColor = [UIColor clearColor];
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_table_nodata"]];
        imgView.translatesAutoresizingMaskIntoConstraints = NO;
        [self.backgroundView addSubview:imgView];
        
        UILabel *label = [[UILabel alloc] init];
        label.translatesAutoresizingMaskIntoConstraints = NO;
        label.backgroundColor = [UIColor clearColor];
        label.textColor = rgb(253, 199, 30);
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:15];
        label.text = title;
        [self.backgroundView addSubview:label];
        
        [label addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:20]];
        
        [imgView addConstraints:@[[NSLayoutConstraint constraintWithItem:imgView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:imgView.image.size.height],
                                  [NSLayoutConstraint constraintWithItem:imgView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:imgView.image.size.width]]];
        
        [self.backgroundView addConstraints:@[[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.backgroundView attribute:NSLayoutAttributeCenterY multiplier:1 constant:0],
                                              [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.backgroundView attribute:NSLayoutAttributeLeft multiplier:1 constant:0],
                                              [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.backgroundView attribute:NSLayoutAttributeRight multiplier:1 constant:0]]];
        
        [self.backgroundView addConstraints:@[[NSLayoutConstraint constraintWithItem:imgView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.backgroundView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0],
                                              [NSLayoutConstraint constraintWithItem:imgView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:label attribute:NSLayoutAttributeTop multiplier:1 constant:0]]];
        
        [self.backgroundView layoutIfNeeded];
    }
}

@end
